var express = require("express");
var router = express.Router();
const fs = require("fs");
const path = require('path')
const multer = require("multer");
var upload = multer({ dest: "uploads/" }); //当前目录下建立文件夹uploads
/* GET home page. */
router.get("/", async function (req, res) {
  res.send('空的')
});

router.post("/index/uploads", upload.single("file"), function (req, res) {
  //   var pash = path.extname(req.file.originalname);
  //   fs.renameSync(req.file.path, req.file.path + pash, (err) => {});
  const files = req.file;
  const filename = new Date().getTime() + ".jpg";
  const filepath = path.join(__dirname, "../uploads/", filename);
  fs.readFile(files.path, (err, data) => {
    fs.writeFile(filepath, data, (results) => {
      // 删除旧文件
      fs.unlinkSync(files.path)
      res.send({ url: "/uploads/" + filename });
    });
  });
});

module.exports = router;
